package com.springboot.controller;

import com.springboot.bean.User;
import com.springboot.bean.UserInfo;
import com.springboot.exception.CustomBusinessException;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.springboot.bean.Student;
import com.springboot.service.StudentService;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;
@Tag(name = "用户Controller")
@RestController
public class TestController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/querystudent", method = RequestMethod.GET)
    @ResponseBody
    public Student queryStudentBySno(String sno) {

        return this.studentService.queryStudentBySno(sno);
    }

    @RequestMapping(value = "/getAllUsers", method = RequestMethod.GET)
    List<User> getAllUsers() {
        return this.studentService.getAllUsers();
    }
    @RequestMapping(value = "/insertstudent", method = RequestMethod.GET)
    public int insertstudent(String sno, String sname) {
        UserInfo s = new UserInfo();
        s.setId(Long.valueOf(sno));
        s.setName(sname);
        s.setAge("18");
        return this.studentService.addUserInfo(s);
    }
    @RequestMapping(value = "/queryuser/{sno}", method = RequestMethod.GET)
    @ResponseBody
    @Operation(summary = "获取用户信息", description  = "根据用户id获取用户信息")
    public UserInfo queryuser(@Parameter(description = "用户id", required = true) @PathVariable(value = "sno") String sno) {
        return this.studentService.selectUser(sno);
    }
    @RequestMapping(value = "/updatestudent", method = RequestMethod.GET)
    public UserInfo updatestudent(@RequestParam("sno") Long sno) {
        UserInfo s = new UserInfo();
        s.setId(sno);
        s.setAge("18");
        return this.studentService.updateUser(s);
    }
    @RequestMapping(value = "/deletestudent", method = RequestMethod.GET)
    public int deletestudent(String sno) {
        return this.studentService.deleteUser(sno);
    }

    @RequestMapping("/index/{senno}")
    String index() {
        throw new RuntimeException("运行时异常");
//        throw new CustomBusinessException("10001", "自定义异常");
//        return "hello spring boot111";
    }
    @GetMapping("async")
    public void testAsync() {
        long start = System.currentTimeMillis();
        logger.info("异步方法开始");

        studentService.asyncMethod();

        logger.info("异步方法结束");
        long end = System.currentTimeMillis();
        logger.info("总耗时：{} ms", end - start);
    }

    @GetMapping("sync")
    public void testSync() {
        long start = System.currentTimeMillis();
        logger.info("同步方法开始");

        studentService.syncMethod();

        logger.info("同步方法结束");
        long end = System.currentTimeMillis();
        logger.info("总耗时：{} ms", end - start);
    }
    @GetMapping("/users/{id}")
    public Mono<UserInfo> getUser(@PathVariable String id) {
        // 将单个用户包装成Mono对象返回
        return Mono.justOrEmpty(this.studentService.selectUser(id));
    }
    @GetMapping("/users")
    public Flux<User> getUsersList() {
        // 将用户列表包装成Flux对象返回
        return Flux.fromIterable(this.studentService.getAllUsers());
    }

}
